package com.ruoyi.gcgl.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.gcgl.mapper.GcQtfksqdMapper;
import com.ruoyi.gcgl.domain.GcFk;
import com.ruoyi.gcgl.domain.GcQtfksqd;
import com.ruoyi.gcgl.service.IGcQtfksqdService;
import com.ruoyi.common.core.text.Convert;

/**
 * 其它付款申请单Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-17
 */
@Service
public class GcQtfksqdServiceImpl implements IGcQtfksqdService 
{
    @Autowired
    private GcQtfksqdMapper gcQtfksqdMapper;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Resource
    IdentityService identityService;

    /**
     * 查询其它付款申请单
     * 
     * @param id 其它付款申请单主键
     * @return 其它付款申请单
     */
    @Override
    public GcQtfksqd selectGcQtfksqdById(Long id)
    {
        return gcQtfksqdMapper.selectGcQtfksqdById(id);
    }

    /**
     * 查询其它付款申请单列表
     * 
     * @param gcQtfksqd 其它付款申请单
     * @return 其它付款申请单
     */
    @Override
    public List<GcQtfksqd> selectGcQtfksqdList(GcQtfksqd gcQtfksqd)
    {
        return gcQtfksqdMapper.selectGcQtfksqdList(gcQtfksqd);
    }

    /**
     * 新增其它付款申请单
     * 
     * @param gcQtfksqd 其它付款申请单
     * @return 结果
     */
    @Override
    public int insertGcQtfksqd(GcQtfksqd gcQtfksqd)
    {
    	int rows = gcQtfksqdMapper.insertGcQtfksqd(gcQtfksqd);
    	// 发起合同审核流程
        identityService.setAuthenticatedUserId(gcQtfksqd.getOwner());
        HashMap<String, Object> variables = new HashMap<>();
        variables.put("applyuserid", gcQtfksqd.getOwner());
        variables.put("level1", gcQtfksqd.getApprovename());
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjgeleibaozhengjin", String.valueOf(gcQtfksqd.getId()), variables);
    	
        GcQtfksqd obj = new GcQtfksqd();
    	obj.setId(gcQtfksqd.getId());
    	obj.setProcessInstanceId(pi.getProcessInstanceId());
    	gcQtfksqdMapper.updateGcQtfksqd(obj);
    	
        return rows;
    }

    /**
     * 修改其它付款申请单
     * 
     * @param gcQtfksqd 其它付款申请单
     * @return 结果
     */
    @Override
    public int updateGcQtfksqd(GcQtfksqd gcQtfksqd)
    {
    	// 发起合同审核流程
//        identityService.setAuthenticatedUserId(gcQtfksqd.getOwner());
//        HashMap<String, Object> variables = new HashMap<>();
//        variables.put("applyuserid", gcQtfksqd.getOwner());
//        variables.put("level1", gcQtfksqd.getApprovename());
//        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjgeleibaozhengjin", String.valueOf(gcQtfksqd.getId()), variables);
//        gcQtfksqd.setProcessInstanceId(pi.getProcessInstanceId());
        return gcQtfksqdMapper.updateGcQtfksqd(gcQtfksqd);
    }

    /**
     * 批量删除其它付款申请单
     * 
     * @param ids 需要删除的其它付款申请单主键
     * @return 结果
     */
    @Override
    public int deleteGcQtfksqdByIds(String ids)
    {
        return gcQtfksqdMapper.deleteGcQtfksqdByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除其它付款申请单信息
     * 
     * @param id 其它付款申请单主键
     * @return 结果
     */
    @Override
    public int deleteGcQtfksqdById(Long id)
    {
        return gcQtfksqdMapper.deleteGcQtfksqdById(id);
    }

	@Override
	public int updateGcQtfksqd4Check(GcQtfksqd gcQtfksqd) {
		return gcQtfksqdMapper.updateGcQtfksqd(gcQtfksqd);
	}
}
